<?php
namespace app\index\controller;

use think\Controller;
use think\View;
use Jssdk\JSSDK;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/1
 * Time: 11:09
 */
class Shop extends CheckMember {

    /**
     * 门店首页数据
     * @return View
     */
    public function index() {
        // 修改资料  直接进入审核页面
        $action = input('param.action', '');
        if ($action == 'edit'){
            $this->redirect('application');
        }

        $member_id = session('member_id');
        $data = $this->local_invoke_api('get_shop_index', ['member_id' => $member_id]);
        if (!$data['data'] || $data['data']['status'] == 0) { // 店铺不存在  进入申请页面
            return view('not_shop');
        }
        $this->assign('data', $data['data']);

        //dump($data['data']);

        if ($data['data']['status'] != 2) {
            return view('audit');
        }

        session('shop_id', $data['data']['id']);
        return view();
    }

    /**
     * 保存店铺申请信息
     * @return \think\response\Json|\think\response\View
     */
    public function application() {
        if (request()->isAjax()) {
            $param = input('param.');
            $param['member_id'] = session('member_id');
            $data = $this->local_invoke_api('save_shop', $param);
            return json($data);
        } else {
            $member_id = session('member_id');
            $member = $this->local_invoke_api('get_member_info', ['member_id' => $member_id]);
            if ($member['data']['certification_status'] != 2) {  // 没有实名认证进入说明页面
                $this->redirect('certification/index');
            }

            $data = $this->local_invoke_api('get_shop_info', ['member_id' => $member_id, 'status' => 0]);

            if (isset($data['data']['status']) && $data['data']['status'] == 2) {
                $this->redirect('index');
            }

            $this->assign('data', $data['data']);
            return view();
        }
    }

    /**
     * 店铺信息
     * @return \think\response\View
     */
    public function shop_info() {
        $member_id = session('member_id');
        $data = $this->local_invoke_api('get_shop_info', ['member_id' => $member_id]);
        if (!$data['data'] || !isset($data['data']['status']) || $data['data']['status'] == 0) { // 店铺不存在  进入申请页面
            return view('not_shop');
        }
        if ($data['data']['status'] != 2) {
            return view('audit');
        }
        $this->assign('data', $data['data']);
        return view();
    }

    /**
     * 编辑经营分类
     * @return \think\response\Json
     */
    public function save_category() {
        $param = input('param.');
        $param['member_id'] = session('member_id');
        $data = $this->local_invoke_api('edit_shop_category', $param);
        return json($data);
    }

    /**
     * 店铺图片
     * @return \think\response\Json|\think\response\View
     */
    public function shop_image() {
        if (request()->isAjax()) {
            $param = input('param.');
            $param['member_id'] = session('member_id');
            $data = $this->local_invoke_api('add_shop_image', $param);
            return json($data);
        } else {
            $param = input('param.');
            $param['member_id'] = session('member_id');
            $data = $this->local_invoke_api('get_shop_image', $param);
            $this->assign('data', $data['data']);
            $this->assign('id', input('param.id'));
            return view();
        }
    }

    /**
     * 设置主图
     * @return \think\response\Json
     */
    public function save_main_image() {
        $param = input('param.');
        $param['member_id'] = session('member_id');
        $data = $this->local_invoke_api('save_main_image', $param);
        return json($data);
    }

    /**
     * 删除图片
     * @return \think\response\Json
     */
    public function delete_image() {
        $param = input('param.');
        $param['member_id'] = session('member_id');
        $data = $this->local_invoke_api('delete_shop_image', $param);
        return json($data);
    }

    /**
     * 管理产品
     * @return \think\response\Json|\think\response\View
     */
    public function shop_product() {
        if (request()->isAjax()) {
            $param = input('param.');
            $param['member_id'] = session('member_id');
            $param['page_type'] = 1;
            $data = $this->local_invoke_api('get_shop_product_list', $param);

            $view = new View();
            $view->assign('shop_id', $param['shop_id']);
            $view->assign('data', $data['data']);
            $tpl = $view->fetch('shop_product_tpl');
            $data['tpl'] = $tpl;
            return json($data);

        } else {
            $param = input('param.');
            $param['member_id'] = session('member_id');
            $data = $this->local_invoke_api('get_shop_category', $param);
            $this->assign('data', $data['data']);
            $this->assign('id', input('param.id'));
            return view();
        }
    }

    /**
     * 补货
     * @return \think\response\Json|\think\response\View
     */
    public function replenishment() {
        if (request()->isAjax()) {
            $param = input('param.');
            $param['member_id'] = session('member_id');
            $data = $this->local_invoke_api('shop_product_replenishment_apply_for', $param);
            return json($data);

        } else {
            $param = input('param.');
            $param['member_id'] = session('member_id');
            $data = $this->local_invoke_api('get_replenishment_product_info', $param);
            $this->assign('data', $data['data']);
            $this->assign('id', input('param.id'));
            $this->assign('shop_id', input('param.shop_id'));
            return view();
        }
    }

    /**
     * 补货记录管理
     * @return \think\response\View
     */
    public function replenishment_list() {
        $param = input('param.');
        $param['member_id'] = session('member_id');
        $data = $this->local_invoke_api('get_replenishment_list', $param);
        $this->assign('data', $data['data']);
        $this->assign('shop_id', $param['id']);
        return view();
    }

    /**
     * 店铺补货确认收货
     * @return \think\response\Json
     */
    public function replenishment_confirm_receipt(){
        $param = input('param.');
        $param['member_id'] = session('member_id');
        $data = $this->local_invoke_api('replenishment_confirm_receipt', $param);
        return json($data);
    }
    /**
     * 店铺补货取消申请
     * @return \think\response\Json
     */
    public function replenishment_cancel(){
        $param = input('param.');
        $param['member_id'] = session('member_id');
        $data = $this->local_invoke_api('replenishment_cancel', $param);
        return json($data);
    }

    /**
     * 抢单页面
     * @return \think\response\View
     */
    public function vie_for_order_list() {
        $param = input('param.');
        $param['member_id'] = session('member_id');
        $data = $this->local_invoke_api('get_vie_for_order_list', $param);
        $this->assign('data', $data['data']);
        $this->assign('shop_id', input('param.id'));
        return view();
    }

    /**
     * 待抢订单详情页面
     * @return \think\response\View
     */
    public function vie_for_order_info() {
        $param = input('param.');
        $param['member_id'] = session('member_id');
        $data = $this->local_invoke_api('get_vie_for_order_info', $param);
        if (empty($data['data'])) {
            $this->redirect('vie_for_order_list', ['shop_id' => $param['shop_id']]);
        }
        $this->assign('data', $data['data']);
        $this->assign('shop_id', input('param.shop_id'));
        return view();
    }

    /**
     * 抢单
     * @return \think\response\View
     */
    public function vie_for_order() {
        $param = input('param.');
        $param['member_id'] = session('member_id');
        $data = $this->local_invoke_api('vie_for_order', $param);
        return json($data);
    }

    /**
     * 店铺订单列表
     * @return \think\response\View
     */
    public function shop_order_list() {
        if (request()->isAjax()) {
            $param = input('param.');
            $param['member_id'] = session('member_id');
            $data = $this->local_invoke_api('get_shop_order_list', $param);

            $view = new View();
            $view->assign('data', $data['data']);
            $view->assign('shop_id', input('param.id'));
            $tpl = $view->fetch('shop_order_list_tpl');

            $data['tpl'] = $tpl;
            return json($data);
        } else {

            return view();
        }
    }

    /**
     * 店铺订单详情
     * @return \think\response\View
     */
    public function shop_order_info() {
        $param = input('param.');
        $param['member_id'] = session('member_id');
        $data = $this->local_invoke_api('get_shop_order_info', $param);
        $this->assign('data', $data['data']);
        $this->assign('shop_id', input('param.shop_id'));
        return view();
    }

    /**
     * 店铺订单添加商品
     * @return \think\response\View
     */
    public function shop_order_add_product() {
        if (request()->isAjax()) {
            $param = input('param.');
            $param['member_id'] = session('member_id');
            $param['page_type'] = 2;
            $data = $this->local_invoke_api('get_shop_product_list', $param);

            $view = new View();
            $view->assign('shop_id', $param['shop_id']);
            $view->assign('data', $data['data']);
            $tpl = $view->fetch('shop_order_add_product_tpl');
            $data['tpl'] = $tpl;
            return json($data);

        } else {
            $param = input('param.');
            $param['member_id'] = session('member_id');
            $data = $this->local_invoke_api('get_shop_category', $param);
            $this->assign('data', $data['data']);
            $this->assign('id', input('param.id'));
            $this->assign('order_id', input('param.order_id'));
            return view();
        }
    }

    /**
     * 保存添加到订单的产品
     * @return \think\response\Json
     */
    public function save_shop_order_product() {
        $param = input('param.');
        $param['member_id'] = session('member_id');
        $data = $this->local_invoke_api('save_shop_order_product', $param);
        return json($data);
    }

    /**
     * 调整产品数量
     * @return \think\response\Json
     */
    public function edit_order_product_num() {
        $param = input('param.');
        $param['member_id'] = session('member_id');
        $data = $this->local_invoke_api('edit_order_product_num', $param);
        return json($data);
    }

    /**
     * 调整产品数量
     * @return \think\response\Json
     */
    public function delete_order_product() {
        $param = input('param.');
        $param['member_id'] = session('member_id');
        $data = $this->local_invoke_api('delete_order_product', $param);
        return json($data);
    }

    /**
     * 推送订单
     * @return \think\response\Json
     */
    public function push_order() {
        $param = input('param.');
        $param['member_id'] = session('member_id');
        $data = $this->local_invoke_api('push_order', $param);
        return json($data);
    }

    public function save_shop_info() {
        $param = input('param.');
        $param['member_id'] = session('member_id');
        $data = $this->local_invoke_api('save_shop_info', $param);
        return json($data);
    }
}